package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.activity.CollegeMoreDetail;
import mainFragment.adapter.ZhaoShengSituationCoverAdapter;
import mainFragment.adapter.ZhaoShengSituationMoreAdapter;
import mainFragment.bean.AdminssionBean;
import mainFragment.bean.CollegeIndexBean;
import mainFragment.bean.ZhaoShengSituationCoverBean;
import mainFragment.bean.ZhaoShengSituationMajorBean;
import mainFragment.bean.ZhaoShengSituationProvBean;
import utils.MyLogUtils;
import view.MyPtrRefresh_LoadView;
import view.ZhaoShengSituationSelector;

/**
 * Created by Administrator on 2017/3/3.
 * 招生情况
 */
@SuppressLint("ValidFragment")
public class ZhaoShengSituation extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private View view;
    private Context context;
    private ListView zhaoshengsituation_lv;
    private View headerView;
    private View footerView;
    private ZhaoShengSituationMoreAdapter zhaoShengSituationMoreAdapter;
    private MyPtrRefresh_LoadView zhaoshengsituation_load_layout;
    private String id;
    private LinearLayout zhaoshengsituation_location_layout;//选择省份
    private TextView zhaoshengsituation_location_tv;//当前省份
    private LinearLayout zhaoshengsituation_major_layout;//选择专业
    private TextView zhaoshengsituation_major_tv;//当前专业
    private RadioGroup zhaoshengsituation_radiogroup;
    private RadioButton zhaoshengsituation_subject_wen;//文科
    private RadioButton zhaoshengsituation_subject_li;//理科
    private final int GET_PRO = 0;//获取省份
    private final int GET_MAJOR = 1;//获取专业
    private final int GET_DATA = 2;//获取列表
    public static final int CHOSE_PROV = 3;//修改省份
    public static final int CHOSE_MAJOR = 4;//修改省份
    private ArrayList<String> proList = new ArrayList<>();//省份列表
    private ArrayList<String> majorList = new ArrayList<>();//专业列表
    private ArrayList<AdminssionBean.MajorListBean> majorListBeanList = new ArrayList<>();//最终列表
    private static final String TAG = "ZhaoShengSituation";
    private ZhaoShengSituationSelector provinceSelector;//省份选择器
    private ZhaoShengSituationSelector majorSelector;//专业选择器
    private String prov = Constant.DEFALUTE_PROV;//当前省份
    private String defaultMajor;//当前专业
    private String type;//文科理科
    private int page;//当前页
    public static final int ONCLICK_ZHAOSHENGSITUATION = 5;//点击了招生详情，需要跳转activity

    private LinearLayout zhaoshengsituation_more_layout;//更多按钮
    private ListView zhaoshengsituation_cover_lv;//往年招生信息列表
    private ImageView zhaoshengsituation_item_more_ima;//更多图片
    private ZhaoShengSituationCoverAdapter zhaoShengSituationCoverAdapter;
    private ArrayList<ZhaoShengSituationCoverBean> zhaoShengSituationCoverBeenList = new ArrayList<>();


    private Handler CollegeMoreDetailHandler;
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Gson gson = new Gson();
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    switch (msg.arg1) {
                        case GET_PRO://获取省
                            ZhaoShengSituationProvBean zhaoShengSituationProvBean = gson.fromJson((String) msg.obj, ZhaoShengSituationProvBean.class);
                            if (zhaoShengSituationProvBean != null) {
                                proList = (ArrayList<String>) zhaoShengSituationProvBean.getProvList();
                                if (proList != null) {
                                    prov = Constant.DEFALUTE_PROV;
                                    initProvinceSelector(proList, prov, myHandler, GET_PRO);
                                }
                            }
                            break;
                        case GET_MAJOR://获取专业
                            ZhaoShengSituationMajorBean zhaoShengSituationMajorBean = gson.fromJson((String) msg.obj, ZhaoShengSituationMajorBean.class);
                            if (zhaoShengSituationMajorBean != null) {
                                majorList = (ArrayList<String>) zhaoShengSituationMajorBean.getMajorList();
                                if (majorList != null) {
                                    majorList.add(0, Constant.NO_MAJOR_FILTER);
                                    defaultMajor = Constant.NO_MAJOR_FILTER;
                                    initMajorSelector(majorList, defaultMajor, myHandler, GET_MAJOR);
                                }
                            }
                            break;
                        case GET_DATA://获取最终list
                            AdminssionBean admissionBean = gson.fromJson((String) msg.obj, AdminssionBean.class);
                            if (admissionBean != null) {
                                List<AdminssionBean.MajorListBean> majorList = admissionBean.getMajorList();
                                if (majorList != null) {
                                    if (majorList.size() > 0) {
                                        if (zhaoshengsituation_lv.getFooterViewsCount() > 0) {
                                            zhaoshengsituation_lv.removeFooterView(footerView);
                                        }
                                        for (int i = 0; i < majorList.size(); i++) {
                                            majorListBeanList.add(majorList.get(i));
                                        }
                                    } else {
                                        footerView = LayoutInflater.from(getMyActivity()).inflate(R.layout.college_situation_no_data, null);
                                        if (zhaoshengsituation_lv.getFooterViewsCount() == 0) {
                                            zhaoshengsituation_lv.addFooterView(footerView);
                                        }
                                    }
                                    if (zhaoShengSituationMoreAdapter != null) {
                                        zhaoShengSituationMoreAdapter.notifyDataSetChanged();
                                        zhaoshengsituation_load_layout.OnRefreshComplete();
                                    }
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                case CHOSE_PROV:
                    prov = (String) msg.obj;
                    zhaoshengsituation_location_tv.setText(prov);//设置好省份
                    initProvinceSelector(proList, prov, myHandler, GET_PRO);//设置好省份
                    provinceSelector.dismiss();
                    page = 1;
                    majorListBeanList.clear();
                    getDataFromInternet(prov, type, defaultMajor, page);
                    break;
                case CHOSE_MAJOR:
                    defaultMajor = (String) msg.obj;
                    zhaoshengsituation_major_tv.setText(defaultMajor);//设置好专业
                    initMajorSelector(majorList, defaultMajor, myHandler, GET_MAJOR);//设置好专业
                    majorSelector.dismiss();
                    page = 1;
                    majorListBeanList.clear();
                    getDataFromInternet(prov, type, defaultMajor, page);
                    break;
                default:
                    break;

            }
        }
    };


    public ZhaoShengSituation(Context context, String id) {
        this.context = context;
        this.id = id;
    }

    public ZhaoShengSituation(Context context, String id, Handler handler) {
        this.context = context;
        this.id = id;
        CollegeMoreDetailHandler = handler;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation, null, false);
        }
        if (getMyActivity() != null) {
            initView();
            type = Constant.DEFAULTE_TYPE;
            defaultMajor = Constant.NO_MAJOR_FILTER;
            page = 1;
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (CollegeMoreDetailHandler != null) {
            Message message = new Message();
            message.what = CollegeMoreDetail.ZHAOSHENGSITUATION_COMPLETE;
            CollegeMoreDetailHandler.sendMessage(message);
        }
        return view;
    }

    private void initView() {
        zhaoshengsituation_cover_lv = (ListView) view.findViewById(R.id.zhaoshengsituation_cover_lv);
        zhaoShengSituationCoverAdapter = new ZhaoShengSituationCoverAdapter(getMyActivity(), zhaoShengSituationCoverBeenList);
        zhaoshengsituation_cover_lv.setAdapter(zhaoShengSituationCoverAdapter);
        zhaoshengsituation_more_layout = (LinearLayout) view.findViewById(R.id.zhaoshengsituation_more_layout);
        zhaoshengsituation_more_layout.setOnClickListener(this);
        zhaoshengsituation_item_more_ima = (ImageView) view.findViewById(R.id.zhaoshengsituation_item_more_ima);

        zhaoshengsituation_load_layout = (MyPtrRefresh_LoadView) view.findViewById(R.id.zhaoshengsituation_load_layout);
        zhaoshengsituation_load_layout.ChangeMode(false, true);
        zhaoshengsituation_load_layout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page++;
                getDataFromInternet(prov, type, defaultMajor, page);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        zhaoshengsituation_lv = (ListView) view.findViewById(R.id.zhaoshengsituation_lv);
        headerView = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation_header, null);
        zhaoshengsituation_location_layout = (LinearLayout) headerView.findViewById(R.id.zhaoshengsituation_location_layout);
        zhaoshengsituation_location_layout.setOnClickListener(this);
        zhaoshengsituation_location_tv = (TextView) headerView.findViewById(R.id.zhaoshengsituation_location_tv);
        zhaoshengsituation_major_layout = (LinearLayout) headerView.findViewById(R.id.zhaoshengsituation_major_layout);
        zhaoshengsituation_major_layout.setOnClickListener(this);
        zhaoshengsituation_major_tv = (TextView) headerView.findViewById(R.id.zhaoshengsituation_major_tv);
        zhaoshengsituation_radiogroup = (RadioGroup) headerView.findViewById(R.id.zhaoshengsituation_radiogroup);
        zhaoshengsituation_subject_wen = (RadioButton) headerView.findViewById(R.id.zhaoshengsituation_subject_wen);
        zhaoshengsituation_subject_li = (RadioButton) headerView.findViewById(R.id.zhaoshengsituation_subject_li);
        for (int i = 0; i < zhaoshengsituation_radiogroup.getChildCount(); i++) {
            zhaoshengsituation_radiogroup.getChildAt(i).setOnClickListener(this);
        }
        zhaoshengsituation_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (!isCollegeIndex()) {
                    int white = getMyActivity().getResources().getColor(R.color.white);
                    int black = getMyActivity().getResources().getColor(R.color.text_color);
                    switch (checkedId) {
                        case R.id.zhaoshengsituation_subject_wen:
                            zhaoshengsituation_subject_wen.setTextColor(white);
                            zhaoshengsituation_subject_li.setTextColor(black);
                            type = "文科";
                            break;
                        case R.id.zhaoshengsituation_subject_li:
                            zhaoshengsituation_subject_wen.setTextColor(black);
                            zhaoshengsituation_subject_li.setTextColor(white);
                            type = "理科";
                            break;
                        default:
                            break;
                    }
                    page = 1;
                    majorListBeanList.clear();
                    MyLogUtils.e(TAG, majorListBeanList.size() + "");
                    getDataFromInternet(prov, type, defaultMajor, page);
                } else {
                    zhaoshengsituation_subject_wen.setChecked(true);
                    zhaoshengsituation_subject_li.setChecked(false);
                    Message msg = new Message();
                    msg.what = ONCLICK_ZHAOSHENGSITUATION;
                    CollegeMoreDetailHandler.sendMessage(msg);
                }
            }
        });
        zhaoshengsituation_lv.addHeaderView(headerView);//添加头部
        zhaoShengSituationMoreAdapter = new ZhaoShengSituationMoreAdapter(getMyActivity(), majorListBeanList);
        zhaoshengsituation_lv.setAdapter(zhaoShengSituationMoreAdapter);
        zhaoshengsituation_lv.setOnItemClickListener(this);
    }


    private void getDataFromInternet(String prov, String type, String major, int page) {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id", id);
            if (!Constant.NO_MAJOR_FILTER.equals(major)) {
                hashMap.put("major", major);
            }
            hashMap.put("type", type);
            hashMap.put("prov", prov);
            hashMap.put("page", String.valueOf(page));
            new InterNetController(getMyActivity(), Constant.ADMISSION, myHandler, hashMap, GET_DATA, false);
        }
    }

    private void getProv() {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id", id);
            new InterNetController(getMyActivity(), Constant.SELECTPROV, myHandler, hashMap, GET_PRO, false);
        }
    }

    private void getMajor() {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id", id);
            hashMap.put("prov", prov);
            new InterNetController(getMyActivity(), Constant.SELECTMAJOR, myHandler, hashMap, GET_MAJOR, false);
        }
    }

    public void setZhaoShengSituationCoverBean(CollegeIndexBean coverBean) {
        getProv();
        getMajor();
        if (coverBean != null && zhaoShengSituationCoverBeenList != null) {
            List<CollegeIndexBean.SurveyArtBean> surveyArt = coverBean.getSurveyArt();//往年文科
            List<CollegeIndexBean.SurveyScienceBean> surveyScience = coverBean.getSurveyScience();//往年理科
            List<CollegeIndexBean.SurveyArtOut> surveyArtOut = coverBean.getSurveyArtOut();//往年文科（市外）
            List<CollegeIndexBean.SurveyScienceOut> surveyScienceOut = coverBean.getSurveyScienceOut();//往年理科(市内)
            zhaoShengSituationCoverBeenList.clear();
            if (surveyArt != null) {
                if (surveyArt.size() > 0) {
                    ZhaoShengSituationCoverBean zhaoShengSituationCoverBeanType = new ZhaoShengSituationCoverBean();
                    if (surveyArtOut.size() > 0) {
                        zhaoShengSituationCoverBeanType.setType("往年【文科市内】录取概况");
                    } else {
                        zhaoShengSituationCoverBeanType.setType("往年【文科】录取概况");
                    }
                    for (int i = 0; i < surveyArt.size(); i++) {
                        ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean zhaoShengSituationCoverBean = new ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean();
                        zhaoShengSituationCoverBean.setCount(surveyArt.get(i).getCount());
                        zhaoShengSituationCoverBean.setPlan(surveyArt.get(i).getPlan());
                        zhaoShengSituationCoverBean.setProvline(surveyArt.get(i).getProvline());
                        zhaoShengSituationCoverBean.setRank(surveyArt.get(i).getRank());
                        zhaoShengSituationCoverBean.setYear(surveyArt.get(i).getYear());
                        zhaoShengSituationCoverBean.setSend(surveyArt.get(i).getSend());
                        zhaoShengSituationCoverBeanType.addItem(zhaoShengSituationCoverBean);
                    }
                    zhaoShengSituationCoverBeenList.add(zhaoShengSituationCoverBeanType);
                }
            }
            if (surveyScience != null) {
                if (surveyScience.size() > 0) {
                    ZhaoShengSituationCoverBean zhaoShengSituationCoverBeanType = new ZhaoShengSituationCoverBean();
                    if (surveyScienceOut.size() > 0) {
                        zhaoShengSituationCoverBeanType.setType("往年【理科市内】录取概况");
                    } else {
                        zhaoShengSituationCoverBeanType.setType("往年【理科】录取概况");
                    }
                    for (int i = 0; i < surveyScience.size(); i++) {
                        ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean zhaoShengSituationCoverBean = new ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean();
                        zhaoShengSituationCoverBean.setCount(surveyScience.get(i).getCount());
                        zhaoShengSituationCoverBean.setPlan(surveyScience.get(i).getPlan());
                        zhaoShengSituationCoverBean.setProvline(surveyScience.get(i).getProvline());
                        zhaoShengSituationCoverBean.setRank(surveyScience.get(i).getRank());
                        zhaoShengSituationCoverBean.setYear(surveyScience.get(i).getYear());
                        zhaoShengSituationCoverBean.setSend(surveyScience.get(i).getSend());
                        zhaoShengSituationCoverBeanType.addItem(zhaoShengSituationCoverBean);
                    }
                    zhaoShengSituationCoverBeenList.add(zhaoShengSituationCoverBeanType);
                }
            }
            if (surveyArtOut != null) {
                if (surveyArtOut.size() > 0) {
                    ZhaoShengSituationCoverBean zhaoShengSituationCoverBeanType = new ZhaoShengSituationCoverBean();
                    zhaoShengSituationCoverBeanType.setType("往年【文科市外】录取概况");
                    for (int i = 0; i < surveyArtOut.size(); i++) {
                        ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean zhaoShengSituationCoverBean = new ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean();
                        zhaoShengSituationCoverBean.setCount(surveyArtOut.get(i).getCount());
                        zhaoShengSituationCoverBean.setPlan(surveyArtOut.get(i).getPlan());
                        zhaoShengSituationCoverBean.setProvline(surveyArtOut.get(i).getProvline());
                        zhaoShengSituationCoverBean.setRank(surveyArtOut.get(i).getRank());
                        zhaoShengSituationCoverBean.setYear(surveyArtOut.get(i).getYear());
                        zhaoShengSituationCoverBean.setSend(surveyArtOut.get(i).getSend());
                        zhaoShengSituationCoverBeanType.addItem(zhaoShengSituationCoverBean);
                    }
                    zhaoShengSituationCoverBeenList.add(zhaoShengSituationCoverBeanType);
                }
            }
            if (surveyScienceOut != null) {
                if (surveyScienceOut.size() > 0) {
                    ZhaoShengSituationCoverBean zhaoShengSituationCoverBeanType = new ZhaoShengSituationCoverBean();
                    zhaoShengSituationCoverBeanType.setType("往年【理科市外】录取概况");
                    for (int i = 0; i < surveyArt.size(); i++) {
                        ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean zhaoShengSituationCoverBean = new ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean();
                        zhaoShengSituationCoverBean.setCount(surveyScienceOut.get(i).getCount());
                        zhaoShengSituationCoverBean.setPlan(surveyScienceOut.get(i).getPlan());
                        zhaoShengSituationCoverBean.setProvline(surveyScienceOut.get(i).getProvline());
                        zhaoShengSituationCoverBean.setRank(surveyScienceOut.get(i).getRank());
                        zhaoShengSituationCoverBean.setYear(surveyScienceOut.get(i).getYear());
                        zhaoShengSituationCoverBean.setSend(surveyScienceOut.get(i).getSend());
                        zhaoShengSituationCoverBeanType.addItem(zhaoShengSituationCoverBean);
                    }
                    zhaoShengSituationCoverBeenList.add(zhaoShengSituationCoverBeanType);
                }
            }
            if (zhaoShengSituationCoverAdapter != null) {
                zhaoShengSituationCoverAdapter.notifyDataSetChanged();
            }
        }
    }

    public void setZhaoShengSituationBean(AdminssionBean adminssionBean) {
        if (majorListBeanList != null) {
            if (adminssionBean == null) {
                getDataFromInternet(Constant.DEFALUTE_PROV, Constant.DEFAULTE_TYPE, Constant.NO_MAJOR_FILTER, 1);
            } else {
                if (adminssionBean != null) {
                    List<AdminssionBean.MajorListBean> majorList = adminssionBean.getMajorList();
                    if (majorList != null) {
                        for (int i = 0; i < majorList.size(); i++) {
                            majorListBeanList.add(majorList.get(i));
                        }
                        if (zhaoShengSituationMoreAdapter != null && zhaoshengsituation_load_layout != null) {
                            zhaoShengSituationMoreAdapter.notifyDataSetChanged();
                            zhaoshengsituation_load_layout.OnRefreshComplete();
                        }
                    }
                }
            }
        }
    }

    private void initProvinceSelector(ArrayList<String> proList, String defaultProvince, Handler myHandler, int flag) {
        if (provinceSelector == null) {
            provinceSelector = new ZhaoShengSituationSelector(getMyActivity(), R.style.DialogTheme);
        }
        provinceSelector.setList(proList, defaultProvince, myHandler, flag);
    }

    private void initMajorSelector(ArrayList<String> proList, String defaultProvince, Handler myHandler, int flag) {
        if (majorSelector == null) {
            majorSelector = new ZhaoShengSituationSelector(getMyActivity(), R.style.DialogTheme);
        }
        majorSelector.setList(proList, defaultProvince, myHandler, flag);
    }

    @Override
    public void onClick(View v) {
        if ("mainFragment.activity.CollegeIndex".equals(getMyActivity().getClass().getName())) {
            //在首页点击时不做反应，直接跳转
            Message msg = new Message();
            msg.what = ONCLICK_ZHAOSHENGSITUATION;
            CollegeMoreDetailHandler.sendMessage(msg);
            return;
        }
        switch (v.getId()) {
            case R.id.zhaoshengsituation_location_layout:
                provinceSelector.show();
                break;
            case R.id.zhaoshengsituation_major_layout:
                majorSelector.show();
                break;
            case R.id.zhaoshengsituation_more_layout:
                //显示更多信息
                MyLogUtils.e(TAG, "" + zhaoshengsituation_cover_lv.getVisibility());
                if (zhaoshengsituation_cover_lv.getVisibility() == View.VISIBLE) {
                    zhaoshengsituation_cover_lv.setVisibility(View.GONE);
                    Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getMyActivity(), R.anim.rotate);
                    hyperspaceJumpAnimation.setDuration(200);
                    hyperspaceJumpAnimation.setRepeatCount(1);
                    hyperspaceJumpAnimation.setFillAfter(true);
                    zhaoshengsituation_item_more_ima.startAnimation(hyperspaceJumpAnimation);
                } else {
                    zhaoshengsituation_cover_lv.setVisibility(View.VISIBLE);
                    Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getMyActivity(), R.anim.rotate_back);
                    hyperspaceJumpAnimation.setDuration(200);
                    hyperspaceJumpAnimation.setRepeatCount(1);
                    hyperspaceJumpAnimation.setFillAfter(true);
                    zhaoshengsituation_item_more_ima.startAnimation(hyperspaceJumpAnimation);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isCollegeIndex()) {
            Message msg = new Message();
            msg.what = ONCLICK_ZHAOSHENGSITUATION;
            CollegeMoreDetailHandler.sendMessage(msg);
        }
    }

    private boolean isCollegeIndex() {
        if ("mainFragment.activity.CollegeIndex".equals(getMyActivity().getClass().getName())) {
            //在首页点击时不做反应，直接跳转
            return true;
        }
        return false;
    }

}
